package com.situ.ymall.mapper;

import com.situ.ymall.entity.Order;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrderMapper {
    @Delete({
        "delete from `order`",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @Insert({
        "insert into `order` (id,user_id)",
            "values (#{id},#{userId})"
    })
    int insert(Order order);

    @Select({
        "select",
        "id, user_id, address_id, time, express, status",
        "from `order`",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results( id = "order",
            value = {
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="address_id", property="addressId", jdbcType=JdbcType.INTEGER),
        @Result(column="time", property="time", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="express", property="express", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column = "id",property = "orderDetails",
            many = @Many(select = "com.situ.ymall.mapper.OrderDetailsMapper.selectByOrderId")),
        //一对一映射
        @Result(column = "address_id",property = "address",
            one = @One(select = "com.situ.ymall.mapper.AddressMapper.selectByPrimaryKey"))
    })
    Order selectByPrimaryKey(String id);

    @Select({
        "select",
        "id, user_id, address_id, time, express, status",
        "from `order`"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="address_id", property="addressId", jdbcType=JdbcType.INTEGER),
        @Result(column="time", property="time", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="express", property="express", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<Order> selectAll();

    @Update({
        "update `order`",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "address_id = #{addressId,jdbcType=INTEGER},",
          "time = #{time,jdbcType=TIMESTAMP},",
          "express = #{express,jdbcType=VARCHAR},",
          "status = #{status,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(Order record);

    @Select({
            "select * from `order` where user_id =  #{userId} order by time desc"
    })
    @ResultMap("order")
    List<Order> selectByUserId(Integer userId);

   @Select({
            "select * from `order` where time < date_sub( now() ,interval 30 minute)  and status = 0"
    })
   @ResultMap("order")
    List<Order> selectInvalid();
}